-- outBonfire
-- Created by huanghr May/25/2016
-- 熄灭的篝火堆

-- 初始化
function initOutBonfire(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 检查下有没有打火石，没有打火石不能操作
            if ItemM.getAmount(ME.user, 7399) < 1 then
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end


            local pos = self.gridData:getPos();
            local lighted = tonumber(self.gridData.lighted);
            if lighted < 1 then
                local function lightFunc()
                    self.gridData.lighted = 1;
                    -- 替换图标
                    local function torchEffect1()
                        playEffect(self, 1139, 0, 15);
                        return 0.5;
                    end

                    self.actionManager:addAction(self.gridData, torchEffect1, "torchEffect1", {}, GRID_ACTION_PHASE_THROW_FLY);

                    self.actionManager:addAction(self.gridData, torchEffect1, "torchEffect2", {}, GRID_ACTION_PHASE_MONSTER_DAMAGED);

                    local function burningFire()
                        doAlert(getLocStr("you_light_bonfire"));
                        local newIconPath = getLevelItemIconPath("bonfire");
                        self.icon:loadTexture(newIconPath);
                        self.iconPath = newIconPath;
                        return 0.1;
                    end

                    self.actionManager:addAction(self.gridData, burningFire, "burningFire", {}, GRID_ACTION_PHASE_MONSTER_UPDATE);
                    EventMgr.fire(event.PLAYER_MOVE);
                end

                local function rummageFunc()
                    DungeonActionM.go("take_bonfire_bonus", pos, 1);
                end

                self:openBuildingView2(lightFunc, rummageFunc, buildingInfo["view_text"],
                    buildingInfo["view_text2"], buildingInfo["dialog1"]);

                return;
            end

            local function takeTorch()
                DungeonActionM.go("take_bonfire_bonus", pos, 2);
            end

            local desc = getLocStr("bonfire_desc");
            local btnText = getLocStr("rummage_button");
            local title = getLocStr("burning_fire");
            self:openBuildingView(takeTorch, -20, desc, btnText, title, self.iconPath);
        end
    end

    -- 建筑图标
    local lightedFlag = tonumber(self.gridData.lighted);
    local iconPath;
    if lightedFlag < 1 then
        iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    else
        iconPath = getLevelItemIconPath("bonfire");
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
